The GUI (Graphic User Interface) is a “point and click” way to interact with a computer. The Windows File Explorer and macOS Finder are examples of GUIs.
The CLI (Command Line Interface) is a text-based way to interact with a computer.
The terminal is another name for the CLI.
mac中,CLI就是terminal,terminal就是CLI; Windows Subsystem for Linux (WSL) 中叫做ubantu (没错就是ubantu server的虚拟机版本).
The CLI is fast, easy to automate, and easy to use remotely.
1.1 Mac OS CLI tools Installation
1.1.1 Install compiler
Open the Terminal application which comes with macOS. Install a compiler.
cmd
xcode-select --install
Notice that this compiler is really Apple LLVM pretending to be g++. Your version might be different.
cmd
g++ --version#Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1# Apple LLVM version 9.0.0 (clang-900.0.38)# Target: x86_64-apple-darwin16.7.0#Thread model: posix# InstalledDir: /Library/Developer/CommandLineTools/usr/bin*/)
如果想要打开而不是进入某个文件夹/文件的话就是 open 以及 wslview. open 是MacOS的指令,wslview 是wsl的指令.
cmd
cd /mnt/c/Usersls# ...(目录里的各种东西)cd 19680 #我的用户名ls # ...(目录里的各种目录和文件, 包括Desktop和Documents)cd 'My Documents'...
1.2.6 Pitfalls(需要注意的隐患)
Avoid Spaces in paths
Avoid paths that contain spaces. Spaces causes problems with some command line tools.
Bad Example
Good Example
EECS 280/
eecs280/
Project 1 Stats/
p1-stats/
Project folder in Windows home directory
WSL中Linux(Ubuntu)有一个独立的home directory.
虽然我们讲了怎么在WSL中ubantu和windows互通文件,但是将代码存储在 Windows 主目录中可能会导致运行速度减慢,因为 WSL 使用network file share(网络文件共享)在 Windows 和 Linux 之间传输文件.
Bad Example
Good Example
/c/mnt/Users/awdeorio ...
/home/awdeorio ...
Root user
避免在 WSL 中以 root 用户身份进行 everyday coding,因为有些程序在以root用户身份运行时无法正常工作. When you first installed Ubuntu, you should have been prompted to create a Linux username and password.
1.3 Basic Commands
A file stores data like C++ source code (main.cpp) or plain text (example.txt).
A directory contains files and other directories. It’s also called a folder. 也就是文件夹
A path is the location of a file or directory. Sometimes we end a directory path with /.(有没有不重要) For example:
前面Accessing Windows files from Linux写过了, macOS 中用 open 来opens a file or directory with the default application; WSL (Windows)中用 wslview 来opens a file or directory with the default application.
The pipe (|) sends the output of the left command to the input of the right command.
把左边命令的输出放进右边命令的输入,然后执行右边命令.
看着有点绕. Here’s an example that searches for .cpp files. The output of ls is piped to the input of grep. 这里是把 ls 也就是打印 working directory 中所有文件的结果弄成一个临时文件让grep查查里面有几个 .cpp, 巧妙地查询了当前 directory中 cpp 文件的数量.
cmd
$ ls | grep cppmain.cppstats.cppstats_tests.cpp
1.5.2 Input redirection <: 用文件作为程序输入
Input redirection sends the contents of a file to the input of a program.
Input redirection is useful for automating program input. 这是程序自动化的重要内容.
In this tutorial, you’ll learn to automate command line tasks using a shell script. Each line of code in a shell script is a command that could be entered at the command line.
现在我们来写一些command用以统计the number of files and directories in the present working directory.
cmd
ls# bin hello hello_css html insta485 insta485generator pyproject.toml testsecho "Count of files and folders:"# Count of files and folders:ls | wc -l# 8
1.7.1 Creating a shell script
现在我们不一行一行打了. 我们创建一个text file called lscount and add the following contents. Notice that shell scripts may omit a file extension. shell scripts的后缀名名会被忽略,并不重要.
cmd
#!/bin/bash## lscount## List files and folders, followed by a count# Stop on errors, print commands# See https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/set -Eeuo pipefailset -x# Listls# Countecho "Count of files and folders:"ls | wc -l
1.7.2 Adding an executable bit
**We can’t yet execute the script because the executable bit is not yet set. The rw means “Read Write”. **
Scripts need to have UNIX line endings to work properly. Windows CRLF line terminators are a problem.
cmd
./bin/lscount# -bash: ./bin/lscount: /bin/bash^M: bad interpreter: No such file or directoryfile ./bin/lscount# bin/lscount: Bourne-Again shell script text executable, ASCII text, with CRLF line terminators
Change Windows line endings to UNIX line endings. 以下三个方法都可以.You might have to install the dos2unix package.
cmd
dos2unix bin/lscount
cmd
fromdos bin/lscount
cmd
sed -ri 's/\r$//' bin/lscount
This is what a correct script file type looks like:
cmd
file bin/lscount# bin/lscount: Bourne-Again shell script text executable, ASCII text